package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.courseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lanhaif
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

//    课程列表
//     完善条件查询带分页

    @GetMapping("pageListCourse/{current}/{limit}")
    public R pageListCourse(
            @PathVariable long current,
            @PathVariable long limit
    ){
//        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        eduCourseService.page(pageCourse,null);
        List<EduCourse> records = pageCourse.getRecords();
        long total = pageCourse.getTotal();

        return R.ok().data("rows",records).data("total",total);

    }


    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageListCourse(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) courseQuery cQuery
    ){
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        Page<EduCourse> pageCourse = new Page<>(current,limit);

        String title = cQuery.getTitle();
        String status = cQuery.getStatus();

        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        eduCourseService.page(pageCourse,wrapper);
        List<EduCourse> records = pageCourse.getRecords();
        long total = pageCourse.getTotal();
        return R.ok().data("rows",records).data("total",total);

    }


    @GetMapping
    public R getCourseLsit(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }


    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
//       返回添加后课程id，为了后面添加大纲使用
        String id=eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }


    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo=eduCourseService.PublishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

//    删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();

    }







}

